package com.socialin.android.hackaton.draw;

import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.SlidingDrawer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.socialin.android.hackaton.draw.drawing.DrawingView;
import com.socialin.android.hackaton.draw.listener.JsonSender;
import com.socialin.android.hackaton.fragment.ChatFragment;
import com.socialin.android.hackaton.fragment.UiFragment;
import com.socialin.picsin.camera.view.CameraViewFroyo;

public class MainActivity extends FragmentActivity {

	private SlidingDrawer chatSlidingDrawer = null;
	
	JsonSender drawingSender = new JsonSender() {

		@Override
		public void onSend(JSONObject json) {
			MessageQueue.getInstance().sendMessage(json.toString());
		}
	};
	JsonSender chatSender = new JsonSender() {

		@Override
		public void onSend(JSONObject json) {
			MessageQueue.getInstance().sendMessage(json.toString());
		}
	};
	
	private CameraViewFroyo cameraView;
	private DrawingView drawingView;
	// http://192.168.43.175:3000/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		drawingView = (DrawingView) findViewById(R.id.drawing_view);
		drawingView.setJsonSender(drawingSender);
		MessageQueue.getInstance().setDrawingReceiver(drawingView);

		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
		ChatFragment chatFragment = new ChatFragment();
		chatFragment.setSlidingDrawer((SlidingDrawer) findViewById(R.id.chat_sliding_drawer));
		chatFragment.setJSONSender(chatSender);
		MessageQueue.getInstance().setChatReceiver(chatFragment);
		ft.add(R.id.chat_frame_id, chatFragment);
		ft.commit();
		
		addUiFragment(drawingView);
	}

	public void addUiFragment(DrawingView drawingView) {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		UiFragment fragment = new UiFragment();
		fragment.setDrawingView(drawingView);
		ft.add(R.id.ui_frame_id, fragment);
		ft.commit();
		initCamera();
	}
	
	private void initCamera()
	{
		android.widget.FrameLayout.LayoutParams layoutParams = new android.widget.FrameLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight());
		FrameLayout cameraContainer = (FrameLayout)findViewById(R.id.camera_container);
		cameraView = new CameraViewFroyo(this, layoutParams);
		Callback surfaceHolder = new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {}
		};
		cameraView.setSurfaceChangeCallback(surfaceHolder);
		cameraContainer.addView((View)cameraView);
		cameraView.setCameraCallBack(drawingView);
		
		findViewById(R.id.addShapeBtn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.e("ex", "onClick");
				cameraView.startPreview();
				drawingView.setDrawMode(DrawingView.DRAW_MODE_SHAPE);
			}
		});
		findViewById(R.id.takePhotoBtn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cameraView.takePictureAndGetRectBitmap(drawingView.getShapeRect());
			}
		});

		Bitmap shapeSizeHandeBm = BitmapFactory.decodeResource(getResources(), R.drawable.red_eye_handle_scale);
		Bitmap shapeScaleHandeBm = BitmapFactory.decodeResource(getResources(), R.drawable.shape_scale_handle);
		
		drawingView.initShapeDrawer(shapeSizeHandeBm, shapeScaleHandeBm);
		
		DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;
        drawingView.setScreenSize(width, height);
	}
}